﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.IO;
using System.Linq;
using System.Net.WebSockets;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Interop;
using Newtonsoft.Json;

namespace DuBaiCoffee2.Model
{
    public class CoffeeMakes
    {
        private static CoffeeMakes instance;
        private CoffeeMakes() { }

        public static CoffeeMakes Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new CoffeeMakes();
                }
                return instance;
            }
        }
        public bool InServiceMenuBol = false;
        /// <summary>
        /// 图片转base64
        /// </summary>
        /// <param name="path">图片地址</param>
        /// <returns></returns>
        public static string SetImg(string path)
        {
            return "image/jpeg;base64," + Convert.ToBase64String(System.IO.File.ReadAllBytes(path));
        }
    
        /// <summary>
        /// 获取网络图片转换为base64
        /// </summary>
        /// <param name="url">网络图片路径   例：http://123.123.123.123/a.jpg</param>
        /// <returns></returns>
        public string GetUrlToBase64(string url)
        {
#pragma warning disable CS0168 // 声明了变量，但从未使用过
            try
            {
                var base64String = "";
                HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
                HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
                string base64sstr = string.Empty;
                using (Stream stream = webResponse.GetResponseStream())
                {
                    var mStream = new MemoryStream();

                    byte[] buffer = new byte[1024];
                    int byteCount;
                    do
                    {
                        byteCount = stream.Read(buffer, 0, buffer.Length);
                        mStream.Write(buffer, 0, byteCount);
                    } while (byteCount > 0);

                    mStream.Seek(0, SeekOrigin.Begin);
                    base64String = Convert.ToBase64String(mStream.ToArray());
                }
                //return base64String;
                WebClient mywebclient = new WebClient();
                byte[] Bytes = mywebclient.DownloadData(url);
                string Base64string = Convert.ToBase64String(Bytes);
                return "data:image/jpeg;base64," + base64String;
            }
            catch (Exception e)
            {
                return "";
            }
#pragma warning restore CS0168 // 声明了变量，但从未使用过
        }
        public void CCM(string id,int Init)
        {
            try
            {

            }
            catch (Exception)
            {
                return;
            }
        }
        public string EnglishRead(string id)
        {
            try
            {
                string name = "";
                switch (id)
                {
                    case "0":
                        name = "Zero";
                        break;
                    case "1":
                        name = "One";
                        break;
                    case "2":
                        name = "Two";
                        break;
                    case "3":
                        name = "Three";
                        break;
                    case "4":
                        name = "Four";
                        break;
                    case "5":
                        name = "Five";
                        break;
                    case "6":
                        name = "Six";
                        break;
                    case "7":
                        name = "Seven";
                        break;
                    case "8":
                        name = "Eight";
                        break;
                    case "9":
                        name = "Nine";
                        break;
                }
                return name;
            }
            catch (Exception)
            {
                return "";
            }
        }
    }
}
